package com.atguigu.service.impl;

import com.atguigu.dao.NginxServiceConfigTcpProxyDao;
import com.atguigu.dao.impl.NginxServiceConfigTcpProxyDaoImpl;
import com.atguigu.pojo.NginxServiceConfigTcpProxy;
import com.atguigu.pojo.Page;
import com.atguigu.service.NginxServiceConfigTcpProxyService;

import java.util.List;

public class NginxServiceConfigTcpProxyServiceImpl implements NginxServiceConfigTcpProxyService {
    NginxServiceConfigTcpProxyDao nginxServiceConfigTcpProxyDao=new NginxServiceConfigTcpProxyDaoImpl();
    @Override
    public void addNginxServiceConfigTcpProxy(NginxServiceConfigTcpProxy nginxServiceConfigTcpProxy) {
        nginxServiceConfigTcpProxyDao.addNginxServiceConfigTcpProxy(nginxServiceConfigTcpProxy);
    }

    @Override
    public void updateNginxServiceConfigTcpProxy(NginxServiceConfigTcpProxy nginxServiceConfigTcpProxy) {
        nginxServiceConfigTcpProxyDao.updateNginxServiceConfigTcpProxy(nginxServiceConfigTcpProxy);
    }

    @Override
    public void deleteNginxServiceConfigTcpProxyById(String id) {
        nginxServiceConfigTcpProxyDao.deleteNginxServiceConfigTcpProxyById(id);
    }

    @Override
    public NginxServiceConfigTcpProxy queryNginxServiceConfigTcpProxyById(String id) {
        return nginxServiceConfigTcpProxyDao.queryNginxServiceConfigTcpProxyById(id);
    }

    @Override
    public List<NginxServiceConfigTcpProxy> queryNginxServiceConfigTcpProxys() {
        return nginxServiceConfigTcpProxyDao.queryNginxServiceConfigTcpProxys();
    }
    @Override
    public List<NginxServiceConfigTcpProxy> queryNginxServiceConfigTcpProxiesByServiceId(String serviceId){
        return nginxServiceConfigTcpProxyDao.queryNginxServiceConfigTcpProxiesByServiceId(serviceId);
    }

    @Override
    public List<NginxServiceConfigTcpProxy> queryNginxServiceConfigTcpProxiesByFrontPort(String frontPort){
        return nginxServiceConfigTcpProxyDao.queryNginxServiceConfigTcpProxiesByFrontPort(frontPort);
    }
    @Override
    public List<NginxServiceConfigTcpProxy> queryNginxServiceConfigTcpProxiesByServiceIdFrontDomainPort(String serviceId,String frontDomain,String frontPort){
        return nginxServiceConfigTcpProxyDao.queryNginxServiceConfigTcpProxiesByServiceIdFrontDomainPort(serviceId,frontDomain,frontPort);
    }
    @Override
    public Page page(Integer pageNo, Integer pageSize) {
        Page<NginxServiceConfigTcpProxy> page=new Page<>();
        page.setPagesize(pageSize);
        Integer pageTotalCount=nginxServiceConfigTcpProxyDao.queryForPageTotalCount();
        page.setPageTotalCount(pageTotalCount);
        Integer pageTotal=pageTotalCount/pageSize;
        if(pageTotalCount % pageSize >0){
            pageTotal++;//总条数,除以一页条数,大于0,页码数+1
        }
        page.setPageTotal(pageTotal);
        System.out.println("pageNo 原来是 is:"+pageNo);
        if(pageNo<1){//防止用户输入量太大
            pageNo=1;
        }else if(pageNo>pageTotal){
            pageNo=pageTotal;
        }
        page.setPageNo(pageNo);//当前页码
        System.out.println("pageNo 现在是 is:"+pageNo);
        System.out.println("pageTotal is:"+pageTotal);
        //要得到的是page的items
        Integer begin=(page.getPageNo()-1)*pageSize;
        List<NginxServiceConfigTcpProxy> items=nginxServiceConfigTcpProxyDao.queryForPageItems(begin,pageSize);
        page.setItems(items);
        return page;
    }
}
